package com.wangnian.wangcai.util;

import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ThsCount {

    private static final Logger logger = LoggerFactory.getLogger(ThsCount.class);
    public static int thsCount = 0;

    public static int teshuzhibiao() {
        if (thsCount > 0) {
            return thsCount;
        }
        int count = runNameTHS("");
        Map<String, String> map = new HashMap<>();
        List<String> tiaojianList = new ArrayList<>();
        tiaojianList.add("J<10;");
        tiaojianList.add("macd<-0.1;");
        tiaojianList.add("BIAS<0;");
        tiaojianList.add("RSI6<30;");
        tiaojianList.add("跌幅小于-2;");
        tiaojianList.add("CCI<-100;");
        tiaojianList.add("WR>90;");
        List<Integer> jieguoList = new ArrayList<>();
        for (String str : tiaojianList) {
            int i1 = runNameTHS(LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")) + " " + str);
            int zhi1 = i1 * 100 / count;
            jieguoList.add(zhi1);
            System.out.println(str + "," + zhi1);
            try {
                Thread.sleep(1500);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        for (int i = 0; i < jieguoList.size(); i++) {
            map.put("${恐慌指标" + (i + 1) + "}", String.valueOf(jieguoList.get(i)));
        }
        int sum = jieguoList.stream().reduce(Integer::sum).get();
        int avgzhi = sum / jieguoList.size();
        map.put("${综合恐慌分}", String.valueOf(avgzhi));
        logger.info("综合恐慌分:{}", avgzhi);
        thsCount = avgzhi;
        return avgzhi;
    }

    public static int runNameTHS(String quesion) {
        HttpRequest httpRequest = HttpRequest.get("http://www.iwencai.com/unifiedwap/unified-wap/result/get-stock-pick");
        httpRequest.form("question", quesion + "上市超过1年；非ST；非科创板；非北交所");
        httpRequest.form("secondary_intent", "stock");
        httpRequest.form("perpage", "4000");
        String respName = httpRequest.execute().body();
        return JSONObject.parseObject(respName).getJSONObject("data").getJSONObject("analyze_data").getInteger("total");
    }

    public static void main(String[] args) {
        System.out.println(runNameTHS("创历史新高"));
    }
}
